package com.itboye.ihomebank.me;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.MyGuanJiaAdapter03;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.bean.ChanKanWeiXiuBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.presenter.RepairPersenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
/**
 * 我的生活管家（待评价）
 *
 */
public class ActivityMyGuanJia03 extends BaseFragment implements Observer{
	XListView guanli_xlistView;
	
	MyGuanJiaAdapter03 adapter;
	
	List<ChanKanWeiXiuBean> listBean;
	
	RepairPersenter repairPersenter;
	TextView imgEmpty;
	String uid ;

	public static  String REFRESH="pingjia";
	@Override
	public int initView() {
		return R.layout.activity_my_guanjia;
	}
	@Override
	public void onMyActivityCreated() {
		showProgressDialog("数据加载中,请稍后", true);
		uid = (String) SPUtils.get(getActivity(), null, SPContants.USER_ID,
				"");
		
		guanli_xlistView.setPullRefreshEnable(false);
		IntentFilter filter=new IntentFilter(REFRESH);
		getActivity().registerReceiver(receiver, filter);
	}
	
	
	BroadcastReceiver receiver=new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			repairPersenter.chanKanWeiXiu(uid, "3");
		}
	}; 
	
	
	@Override
	public void onStart() {
		super.onStart();
		repairPersenter=new RepairPersenter(this);
		repairPersenter.chanKanWeiXiu(uid, "3");
		
		listBean=new ArrayList<ChanKanWeiXiuBean>();
		adapter=new MyGuanJiaAdapter03(getActivity(), listBean, R.layout.item_my_guanjia);
		guanli_xlistView.setAdapter(adapter);
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		// ByAlert.alert(entity.getData());
		if (entity != null) {
			if (!entity.getCode().equals(0)) {
				if (entity.getEventType() == RepairPersenter.weixiuZhuangTai_success) {
					List<ChanKanWeiXiuBean> bean=(List<ChanKanWeiXiuBean>) entity.getData();
					if (bean != null) {
						if (bean.size() <= 0) {
							imgEmpty.setVisibility(View.VISIBLE);
						}else{
							imgEmpty.setVisibility(View.GONE);
						}
					}if (bean.size() <= 0) {
						imgEmpty.setVisibility(View.VISIBLE);
					}
					listBean.addAll(bean);
					adapter.notifyDataSetChanged();
					
				} else if (entity.getEventType() == RepairPersenter.weixiuZhuangTai_fail) {
					ByAlert.alert(entity.getData());
				}
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

}
